<template>
  <base-breadcrumb>
    <template>
      <div class="table-page-search-wrapper">
        <!-- <a-button type="primary" @click="submit">基本信息保存</a-button> -->
        <a-card>
          <titleName title="基本信息"></titleName>
          <a-form-model ref="ruleForm" :model="form" :rules="rules">
            <a-row :gutter="24">
              <a-col :md="16" :sm="16">
                <a-form-model-item label="所属单位" prop="projectId" :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }" :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }">
                  <org-tree-select v-model="form.projectId" isAutoSelectFirst checkStrictly @select="selectOrg"
                                   disabled
                  @getProjectInfo="getProjectInfo"
                  ></org-tree-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="8" :sm="8">
                <a-form-model-item label="名称" prop="name" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 17 }, sm: { span: 17 } }">
                  <BaseInput placeholder="请输入" v-model="form.name"  />
                </a-form-model-item>
              </a-col>
              <a-col :md="8" :sm="8">
                <a-form-model-item label="交底组织人" prop="organizerId" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <a-select show-search placeholder="请选择" v-model="form.organizerId">
                    <a-select-option v-for="i in organizePeopleList" :key="i.employeeId" :value="i.employeeId">{{ i.employeeName }}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="8" :sm="8">
                <a-form-model-item label="交底日期" prop="confessDate" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <a-date-picker v-model="form.confessDate" @change="(date, dateString) => getTime(date, dateString, 'form', 'confessDate')" />
                </a-form-model-item>
              </a-col>
              <a-col :md="8" :sm="8">
                <a-form-model-item label="中标时间"  :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <a-input disabled v-model="winningTime"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :md="24" :sm="24">
                <a-form-model-item label="参与人员" prop="participants" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <BaseTextarea :auto-size="{ minRows: 3, maxRows: 5 }" v-model="form.participants" />
                </a-form-model-item>
              </a-col>
              <a-col :md="24" :sm="24">
                <a-form-model-item label="项目重难点" prop="participants" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <BaseTextarea :auto-size="{ minRows: 3, maxRows: 5 }" v-model="form.projectDifficultPoints" />
                </a-form-model-item>
              </a-col>
              <a-col :md="24" :sm="24">
                <a-form-model-item label="附件上传" :labelCol="{ lg: { span: 3 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                  <l-table v-if="isUpdate" @fileArrysData="fileArrysData" :uploadArrys="form.reviewFileInfoVOList" businessCode="BM08"></l-table>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
        <div class="tool-btns mb20 page-btn-right-top">
          <a-button type="primary" :loading="loading.DRAFT" @click="submitForm('DRAFT')">保存草稿</a-button>
          <a-button type="primary" :loading="loading.SUBMITTED" @click="submitForm('SUBMITTED')">提交</a-button>
          <a-button @click="closeForm()">关闭</a-button>
        </div>
      </div>
    </template>
    <selectApprover
            :business-id="businessId"
            :process-config="setProcessConfig"
    />
  </base-breadcrumb>
</template>

<script>
import { detailed, rewrite, eliminate } from '@/api/businessAffairs/throwMark'
import { TreeSelect } from 'ant-design-vue'
import { getOrganizePeopleList } from '@/api/safetySupervision/emergencyDrill'
import { isAttachment } from '@/utils/util'
const SHOW_PARENT = TreeSelect.SHOW_PARENT
export default {
  name: 'a' + Date.now(),
  async mounted() {
    this.processConfig = await this.$getConfigFn('comm_tender_disclosure_new')
    this.id = this.$route.query.id || ''
    this.$route.meta.title = this.id == '' ? '投标交底记录新增' : '投标交底记录编辑'
    if (this.$route.query.id) {
      this.isUpdate = false
      detailed(this.$route.query.id).then((res) => {
        this.isUpdate = true
        if (res.code === 200) {
          this.form = { ...res.data, project: { key: res.data.projectId, lable: res.data.projectName } }
          this.form.creatorName = this.$userInfo().name
          console.log(this.form)
          this.getOrganizePeopleList()
        } else {
          this.$message.error('获取信息失败')
        }
      })
    }
  },
  data() {
    this.SHOW_PARENT = SHOW_PARENT

    return {
      businessId: '',
      processConfig: 4,
      setProcessConfig: null,
      winningTime: '',
      id: '',
      isUpdate: true,
      loading: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      rules: {
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        organizerId: [{ required: true, message: '请选择信息', trigger: 'change' }],
        confessDate: [{ required: true, message: '请选择日期', trigger: 'change' }],
        participants: [{ required: true, message: '请输入信息', trigger: 'blur' }],
        projectDifficultPoints: [{ required: true, message: '请输入项目重难点', trigger: 'blur' }],
      },
      form: { name: '', confessDate: null, project: { key: '', lable: '' }, projectId: '', projectDifficultPoints: '' },

      organizePeopleList: []
    }
  },
  methods: {
    getProjectInfo(val) {
      this.winningTime = val.winningTime
    },
    getTime(date, dateString, type, name) {
      this[type][name] = dateString
      console.log(this[type][name])
    },
    selectOrg(val) {
      if (val && val.length) {
        console.log('val', val)
        this.form.project = {
          key: val[val.length - 1].projectId,
          label: val[val.length - 1].projectName
        }
        this.form.branchComId = val[val.length - 2].orgId
        console.log('this.form.project', this.form.project)
        this.getOrganizePeopleList()
      }
    },
    // // 交底组织人树选择
    // test(val) {
    //     this.form.organizer = val
    // },
    //获取项目下的人员列表
    getOrganizePeopleList() {
      if (!this.id) {
        this.$nextTick(() => {
          console.log('this.form.project', this.form.project)
          this.form.name = this.form.project.label + '投标交底记录'
        })
      }
      // this.form.projectId = this.form.projectId.key
      console.log(this.form)
      let params = {
        id: this.form.projectId
      }
      console.log(params)
      getOrganizePeopleList(params).then((res) => {
        let obj = {}
        res.data = res.data.reduce((cur, next) => {
          obj[next.employeeId] ? '' : (obj[next.employeeId] = true && cur.push(next))
          return cur
        }, []) //设置cur默认类型为数组，并且初始值为空的数组
        console.log(res.data)
        this.organizePeopleList = res.data || []
      })

      console.log(this.organizePeopleList)
    },
    handleChange({ file, fileList }) {
      if (file.status !== 'uploading') {
        console.log(file, fileList)
      }
    },
    fileArrysData(val) {
      this.form.reviewFileInfoVOList = val
    },
    // 删除草稿
    deleteDraft(e) {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '确定是否删除数据？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          return eliminate({ id: that.id })
            .then((res) => {
              that.$message.success('删除成功')
              that.closeForm()
            })
            .catch((res) => {
              that.$message.warning('删除失败！')
            })
        }
      })
    },
    // // 删除
    // deleteDraft() {
    //     eliminate({ id: this.id }).then((res) => {
    //         if (res.data.code === 200) {
    //             this.$message.success('删除成功')
    //         } else {
    //             this.$message.error('删除失败')
    //         }
    //         // this.$multiTab.close(this.$route.fullpath)
    //         this.closeForm()
    //     })
    // },
    closeForm() {
      this.$multiTab.close(this.$route.fullpath)
      //  history.go(-1)
    },
    // 提交
    submitForm(auditStatus) {
      this.form.auditStatus = auditStatus
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (isAttachment(this, auditStatus, this.form.reviewFileInfoVOList)) return
          const cb = ()=>{
            this.loading[auditStatus] = true
            let obj = {
              ...this.form,
              id: this.$route.query.id
            }
            rewrite(obj).then((res) => {
              this.$refs.ruleForm.resetFields()
              if (res.code === 200) {

                if(auditStatus === 'SUBMITTED') {
                  this.$message.success('提交成功')
                  this.businessId = res.data.id || this.form.id;
                  this.setProcessConfig = this.processConfig;
                }else {
                  this.$message.success('保存草稿成功')
                  this.closeForm()
                }
              } else {
                this.$message.warning('失败')
              }
            })
          }
          if(auditStatus === 'DRAFT') {
            cb()
            return
          }
          this.submitApprovalConfirm( this.processConfig, (flag)=> {
            if (!flag) {
              this.loading[auditStatus] = false
              return
            }
            cb()
          })
        } else {
          this.$message.warning("请填写完所有必填项信息")
          return false
        }
      })
    }
    // // 获取交底日期
    // disclose(a, b) {
    //     this.form.confessDate = b
    // },
  }
}
</script>

<style lang="less" scoped>
@deep: ~'>>>';
.tool-btns {
  display: flex;
  justify-content: flex-end;
  @{deep} .ant-btn + .ant-btn {
    margin-left: 12px;
  }
}
</style>
